home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindspring.com!usenet
- From: rudd@mindspring.com (Justin Rudd)
- Newsgroups: comp.lang.c++
- Subject: Re: sprintf() in C++
- Date: Wed, 10 Apr 1996 02:08:27 GMT
- Organization: MindSpring Enterprises
- Message-ID: <4kf5bg$u8a@mule2.mindspring.com>
- References: <4kene4$nkb@post.gsfc.nasa.gov>
- Reply-To: rudd@mindspring.com
- NNTP-Posting-Host: rudd.mindspring.com
- X-Newsreader: Forte Free Agent v0.55
-
- tycho <vonrosen@nssdca.gsfc.nasa.gov> wrote:
-
- >I could use some help with regard to sprintf() in C++. It seems that
- >cout and cin were invented to avoid printf() and scanf() because the
- >latter have variable argument lists and hence can't be checked against
- >function prototypes. But is there any corresponding C++ approach to
- >sprintf() and sscanf()? When I look at various string class functions
- >I don't see what to use instead of sprintf() and sscanf(). But when I
- >look at my C code, I'm using sprintf() and sscanf() all the time. It
- >seems very clumsy to cast a string object to a character array, use
- >sprintf() to write to it and then convert the result back to a string
- >object. And, of course, using sprintf() means that the compiler can't
- >check against a prototype. So what am I missing? I have examined a
- >number of standard texts and they don't seem to ever go into this when
- >talking about strings. K&R discuss sprintf() and sscanf() under the
- >heading of "In-memory Format Conversion", but these functions do much
- >more than that.
-
- >As a newcomer to C++ I will appreciate any assistance you can give.
-
-
- If you are looking for a C++ way to format a string like sprintf() I
- recommend using the strstream class. It is very simple to use.
-
- //small example
-
- char msg[500];
-
- ostrstream stream(msg,500);
-
- stream << "An Int Value" << 10 << endl;
- stream << "A Float Value" << 10.0f << endl;
- stream << "Really Simple stuff" << ends << endl;
-
- cout << msg;
-
-
- Justin Rudd
- rudd@mindspring.com
- =======================================
- It'll work...trust me ;-)
- =======================================
-
-
-